//
//  YBInviteViewController.m
//  Nectar
//
//  Created by famer on 2023/11/8.
//  Copyright © 2023 asance. All rights reserved.
//

#import "YBInviteViewController+LayoutConstraints.h"
#import "YBNavigationBar.h"
#import "RevDataGetMyInviteInfo.h"
#import "YBTostView.h"

@interface YBInviteViewController ()

@end

@implementation YBInviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = kWhiteColor;
    
    [self.view addSubview:self.navigationBar];
    [self.view addSubview:self.tipsContentView];
    [self.tipsContentView addSubview:self.tipsImageView];
    [self.tipsContentView addSubview:self.tips1Label];
    [self.tipsContentView addSubview:self.tips2Label];
    
    [self.tipsContentView addSubview:self.tipsRateContentView];
    [self.tipsRateContentView addSubview:self.tipsRateLabel];
    [self.tipsRateContentView addSubview:self.tips3Label];
    
    [self.view addSubview:self.linkContentView];
    [self.linkContentView addSubview:self.linkLabel];
    [self.linkContentView addSubview:self.linkValueLabel];
    [self.linkContentView addSubview:self.linkButton];
    
    [self.view addSubview:self.shareContentView];
    [self.shareContentView addSubview:self.shareLabel];
    [self.shareContentView addSubview:self.shareButtonContentView];
    [self.shareContentView addSubview:self.shareButton];
    
    [self.shareButtonContentView addSubview:self.shareImageView];
    [self.shareButtonContentView addSubview:self.shareValueLabel];
    
    [self setLayoutConstraints];
    
    [self updateUI];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
    [self.navigationBar resetBarItemState];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self getMyInviteInfo];
}

#pragma mark - Private Method
- (void)updateUI{
    self.tipsContentView.clipsToBounds = YES;
    self.tipsContentView.layer.cornerRadius = 10;
    
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = self.tipsContentView.bounds;
    UIColor *startColor = [UIColor hexColor:@"6376A8"];
    UIColor *endColor = [UIColor hexColor:@"7D87B4"];
    gradientLayer.colors = @[(id)startColor.CGColor, (id)endColor.CGColor];
    gradientLayer.startPoint = CGPointMake(0, 0);
    gradientLayer.endPoint = CGPointMake(1, 1);
    [self.tipsContentView.layer insertSublayer:gradientLayer atIndex:0];

    self.linkContentView.clipsToBounds = YES;
    self.linkContentView.layer.borderColor = [UIColor hexColor:@"F6F5F6"].CGColor;
    self.linkContentView.layer.borderWidth = 1;
    self.linkContentView.layer.cornerRadius = 6;
    
    self.shareContentView.clipsToBounds = YES;
    self.shareContentView.layer.borderColor = [UIColor hexColor:@"F6F5F6"].CGColor;
    self.shareContentView.layer.borderWidth = 1;
    self.shareContentView.layer.cornerRadius = 6;
}

- (void)getMyInviteInfo{
    [SVProgressHUD show];
    self.view.userInteractionEnabled = NO;
    NSMutableDictionary *params = [NSDictionary baseReqParams];
    NSString *urlString = [NSString stringWithFormat:@"%@",YB_URL_SET_MY_INVITE];
    
    [[YBHttpRequestManager shareInstance] Post:urlString params:params success:^(NSObject *resultObject) {
        NSLog(@"getMyInviteInfo: %@",resultObject);
        
        self.view.userInteractionEnabled = YES;
        [SVProgressHUD dismiss];
        
        //判断网络是否返回错误信息
        YBResponseModel *obj = [YBResponseModel initWithResponseObject:(NSDictionary *)resultObject];
        if([obj success]){
            NSLog(@"-----%@", obj.data);
            //            Hey, I\'m currently using a super easy-to-use digital currency wallet - raw wallet.\n\nNot only can I manage my own digital assets, but what\'s even cooler is that I can also earn USDT rewards while using it. Come and try it.
            
            RevDataGetMyInviteInfo *result = [RevDataGetMyInviteInfo mj_objectWithKeyValues: obj.data];
            
            NSLog(@"------>%@", result.inviteCode);
            self.tipsRateLabel.text = [result.incomeRate toFinancePercentString];
            self.linkValueLabel.text = [NSString stringWithFormat:@"https://www.rawwallet.net?referrer=%@",result.inviteCode];
        }
        
    } failure:^(NSError *error) {
        self.view.userInteractionEnabled = YES;
        YBErrorModel *yerror = [YBErrorModel ErrorObjForNSError:error];
    }];
}

- (void)copyLinkAction{
    [YBTostView showWithTitle:@"Copy success"];
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = [NSString stringWithFormat:@"%@",self.linkValueLabel.text];
}

- (void)shareAction{
    // 要分享的内容
    NSString *tips = @"Hey, I\'m currently using a super easy-to-use digital currency wallet - raw wallet.\n\nNot only can I manage my own digital assets, but what\'s even cooler is that I can also earn USDT rewards while using it. Come and try it.";
    NSString *link = self.linkValueLabel.text;
    NSString *textToShare = [NSString stringWithFormat:@"%@\n\n%@",tips,link];
    NSArray *itemsToShare = @[textToShare];

    // 创建分享视图控制器
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:itemsToShare applicationActivities:nil];

    // 设置在iPad上弹出的位置（可选）
    activityViewController.popoverPresentationController.sourceView = self.view;

    // 显示分享视图控制器
    [self presentViewController:activityViewController animated:YES completion:nil];
}

#pragma mark - Getter Setter
YBViewGetter(tipsContentView, _tipsContentView, [UIColor hexColor:@"6376A8"])
YBViewGetter(tipsRateContentView, _tipsRateContentView, kClearColor)
YBViewGetter(linkContentView, _linkContentView, [UIColor hexColor:@"FFFFFF"])
YBViewGetter(shareContentView, _shareContentView, [UIColor hexColor:@"FFFFFF"])
YBViewGetter(shareButtonContentView, _shareButtonContentView, [UIColor hexColor:@"FFFFFF"])

YBImageViewGetter(tipsImageView, _tipsImageView, [UIImage imageNamed:@"icon_invite_large"])
YBImageViewGetter(shareImageView, _shareImageView, [UIImage imageNamed:@"icon_share_fill"])

YBLabelGetter(tips1Label, _tips1Label, [UIFont systemFontOfSize:12], @"Invite your friends and family to use raw wallet", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(tips2Label, _tips2Label, [UIFont systemFontOfSize:12], @"You will receive an invitation reward equal to", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(tipsRateLabel, _tipsRateLabel, [UIFont systemFontOfSize:20], @"20%", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(tips3Label, _tips3Label, [UIFont systemFontOfSize:12], @"of their 'tansfer in earn'!", kWhiteColor, NSTextAlignmentCenter);
YBLabelGetter(linkLabel, _linkLabel, [UIFont systemFontOfSize:12], @"Your invitation link", [UIColor hexColor:@"84828C"], NSTextAlignmentLeft);
YBLabelGetter(linkValueLabel, _linkValueLabel, [UIFont systemFontOfSize:14], @"*", kThemeBlackColor, NSTextAlignmentLeft);
YBLabelGetter(shareLabel, _shareLabel, [UIFont systemFontOfSize:12], @"Share to social media", [UIColor hexColor:@"84828C"], NSTextAlignmentLeft);
YBLabelGetter(shareValueLabel, _shareValueLabel, [UIFont systemFontOfSize:14], @"Share to social media", [UIColor hexColor:@"6376A8"], NSTextAlignmentLeft);

- (UIButton *)linkButton{
    if(!_linkButton){
        _linkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _linkButton.backgroundColor = kWhiteColor;
        _linkButton.layer.borderColor = [UIColor hexColor:@"6376A8"].CGColor;
        _linkButton.layer.borderWidth = 1;
        _linkButton.layer.cornerRadius = 6;
        [_linkButton.titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_linkButton setTitleColor:[UIColor hexColor:@"6376A8"] forState:UIControlStateNormal];
        [_linkButton setTitleColor:[UIColor hexColor:@"6376A8"] forState:UIControlStateSelected];
        [_linkButton setTitle:@"Copy link" forState:UIControlStateNormal];
        [_linkButton setTitle:@"Copy link" forState:UIControlStateSelected];
        [_linkButton addTarget:self action:@selector(copyLinkAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _linkButton;
}

- (UIButton *)shareButton{
    if(!_shareButton){
        _shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _shareButton.backgroundColor = kClearColor;
        _shareButton.layer.borderColor = [UIColor hexColor:@"6376A8"].CGColor;
        _shareButton.layer.borderWidth = 1;
        _shareButton.layer.cornerRadius = 6;
        [_shareButton setTitle:@"" forState:UIControlStateNormal];
        [_shareButton setTitle:@"" forState:UIControlStateSelected];
        [_shareButton addTarget:self action:@selector(shareAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _shareButton;
}

- (YBNavigationBar *)navigationBar{
    if(!_navigationBar){
        _navigationBar = [[YBNavigationBar alloc] initWithFrame:CGRectZero];
        _navigationBar.backgroundColor = kWhiteColor;
        [_navigationBar setTitle:@"Wallet under identity" titleColor:kThemeBlackColor];
        [_navigationBar setLeftBarImage:@"icon_nav_back" forState:UIControlStateNormal];
        __weak __typeof(&*self)weakSelf = self;
        _navigationBar.onLeftBarButtonBlock = ^(){
            __strong __typeof(&*weakSelf)strongSelf = weakSelf;
            if(!strongSelf) return;
            
            [strongSelf.navigationController popViewControllerAnimated:YES];
        };
    }
    return _navigationBar;
}

@end
